<template>
  <div class="login-page">
    <!-- 导航栏 -->
    <nav class="navbar">
      <div class="logo">
        <span class="logo-text">快递面单录入系统</span>
      </div>
    </nav>
    
    <!-- 主内容区域 -->
    <div class="main-content">
      <div class="container">
        <div class="header">
          <h1>欢迎</h1>
          <p>用户登录</p>
        </div>
        
        <div class="form-container">
          <div v-if="errorMessage" class="error-message" style="display: block;">
            {{ errorMessage }}
          </div>
          
          <div class="form-group">
            <label for="username">用户名</label>
            <input 
              type="text" 
              id="username" 
              v-model="username" 
              placeholder="请输入用户名"
              @keyup.enter="handleLogin"
            >
          </div>
  
          <div class="form-group">
            <label for="password">密码</label>
            <input 
              type="password" 
              id="password" 
              v-model="password" 
              placeholder="请输入密码"
              @keyup.enter="handleLogin"
            >
          </div>
          
          <button class="login-btn" @click="handleLogin" :disabled="loading">
            {{ loading ? '登录中...' : '登录' }}
          </button>
          <button class="login-btn" @click="goToRegister">
            注册
          </button>
        </div>
      </div>
    </div>
    
    <div class="footer">
      <p>2025 ©523300264225徐烨</p>
      <p>J2EE程序设计-快递面单录入系统</p>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { authAPI } from '../api'

const router = useRouter()
const username = ref('')
const password = ref('')
const errorMessage = ref('')
const loading = ref(false)

const handleLogin = async () => {
  if (!username.value.trim() || !password.value.trim()) {
    errorMessage.value = '用户名和密码不能为空'
    return
  }

  loading.value = true
  errorMessage.value = ''

  try {
    const response = await authAPI.login(username.value.trim(), password.value.trim())
    if (response.success) {
      sessionStorage.setItem('currentUser', JSON.stringify({
        username: response.data.username,
        email: response.data.email || ''
      }))
      // 2. 存储 JWT token
      sessionStorage.setItem('authToken', response.data.token)
      router.push('/home')
    } else {
      errorMessage.value = response.message || '登录失败'
    }
  } catch (error) {
    console.error('登录错误:', error)
    errorMessage.value = error.response?.data?.message || error.message || '登录失败，请稍后重试'
  } finally {
    loading.value = false
  }
}

const goToRegister = () => {
  router.push('/register')
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  background: linear-gradient(to right, #ffffff);
  color: #333;
  text-align: center;
  padding: 30px 20px;
}

.header h1 {
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 10px;
}

.header p {
  font-size: 16px;
  opacity: 0.9;
}

.error-message {
  color: #ff4444;
  text-align: center;
  margin-bottom: 20px;
  padding: 10px;
  background-color: #ffebee;
  border-radius: 5px;
  border: 1px solid #ffcdd2;
}
</style>

